package cn.iocoder.yudao.module.polling.service.pollingproject;

import cn.iocoder.yudao.module.polling.controller.admin.pollingproject.vo.*;
import cn.iocoder.yudao.module.polling.convert.pollingproject.PollingProjectConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingproject.PollingProjectDO;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingproject.PollingProjectMapper;
import cn.iocoder.yudao.module.polling.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 巡检项目 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class PollingProjectServiceImpl implements PollingProjectService {

    @Resource
    private PollingProjectMapper pollingProjectMapper;

    @Override
    public Integer createPollingProject(PollingProjectCreateReqVO createReqVO) {
        // 插入
        PollingProjectDO pollingProject = PollingProjectConvert.INSTANCE.convert(createReqVO);
        pollingProjectMapper.insert(pollingProject);
        // 返回
        return pollingProject.getId();
    }

    @Override
    public void updatePollingProject(PollingProjectUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePollingProjectExists(updateReqVO.getId());
        // 更新
        PollingProjectDO updateObj = PollingProjectConvert.INSTANCE.convert(updateReqVO);
        pollingProjectMapper.updateById(updateObj);
    }

    @Override
    public void deletePollingProject(Integer id) {
        // 校验存在
        this.validatePollingProjectExists(id);
        // 删除
        pollingProjectMapper.deleteById(id);
    }

    private void validatePollingProjectExists(Integer id) {
        if (pollingProjectMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public PollingProjectDO getPollingProject(Integer id) {
        return pollingProjectMapper.selectById(id);
    }

    @Override
    public List<PollingProjectDO> getPollingProjectList(Collection<Integer> ids) {
        return pollingProjectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PollingProjectDO> getPollingProjectPage(PollingProjectPageReqVO pageReqVO) {
        return pollingProjectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PollingProjectDO> getPollingProjectList(PollingProjectExportReqVO exportReqVO) {
        return pollingProjectMapper.selectList(exportReqVO);
    }

    @Override
    public List<PollingProjectIOVO> selectByMyList(PollingProjectIOVO pollingProjectIOVO) {
        return pollingProjectMapper.selectByMyList(pollingProjectIOVO);
    }

    @Override
    public List<PollingProjectIOVO> selectProjectByBzId(PollingProjectIOVO pollingProjectIOVO) {
        return pollingProjectMapper.selectProjectByBzId(pollingProjectIOVO);
    }

}
